prost-reflect
A protobuf library extending prost
with reflection support and dynamic messages.
Usage
This crate provides support for dynamic protobuf messages. These are useful when the protobuf type definition is not known ahead of time.
The main entry points into the API of this crate are:
DescriptorPool
wraps aFileDescriptorSet
output by the protobuf compiler to provide an API for inspecting type definitions.DynamicMessage
provides encoding, decoding and reflection of an arbitrary protobuf message definition described by aMessageDescriptor
.
Example - decoding
DynamicMessage
does not implement Default
since it needs a message descriptor to
function. To decode a protobuf byte stream into an instance of this type, use DynamicMessage::decode
to create a default value for the MessageDescriptor
instance and merge into it:
use Message;
use FileDescriptorSet;
use ;
let pool = decode.unwrap;
let message_descriptor = pool.get_message_by_name.unwrap;
let dynamic_message = decode.unwrap;
assert_eq!;
Example - JSON mapping
When the serde
feature is enabled, DynamicMessage
can be deserialized to and from the
canonical JSON mapping
defined for protobuf messages.
use Message;
use ;
use Deserializer;
let pool = decode.unwrap;
let message_descriptor = pool.get_message_by_name.unwrap;
let json = r#"{ "foo": 150 }"#;
let mut deserializer = from_str;
let dynamic_message = deserialize.unwrap;
deserializer.end.unwrap;
assert_eq!;
Example - implementing ReflectMessage
The ReflectMessage
trait provides a .descriptor()
method to get type information for a message. It is implemented for DynamicMessage
and the well-known-types provided by prost-types
.
When the derive
feature is enabled, it can be derived for Message
implementations. The
derive macro takes the following parameters:
Name | Value |
---|---|
descriptor_pool | An expression that resolves to a DescriptorPool containing the message type. The descriptor should be cached to avoid re-building it. Either this or file_descriptor_pool_bytes must be set |
file_descriptor_pool_bytes | An expression that resolves to an implementation of Buf containing an encoded file descriptor set. This will be automatically added to the global descriptor pool the first time ReflectMessage::descriptor() is called. |
message_name | The full name of the message, used to look it up within DescriptorPool . |
use Message;
use ;
use Lazy;
static DESCRIPTOR_POOL:
= new;
let message = MyMessage ;
assert_eq!;
If you are using prost-build
, the prost-reflect-build
crate provides helpers to generate ReflectMessage
implementations:
.compile_protos
.unwrap;
new
Minimum Supported Rust Version
Rust 1.64 or higher.
The minimum supported Rust version may be changed in the future, but it will be done with a minor version bump.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.